// A set of useful fragment shader function all-in-one

#define PI   3.1415926535897932384626433832795

vec2 fvLatLong(vec3 v) {
  v = normalize(v);
  float theta = acos(v.z);
  
  float phi = atan(v.y, v.x) + PI;
  return vec2(phi, theta) * vec2(0.1591549, 0.6366198);
}

vec4 fvComputeBlur(vec2 uv, float accessibility)
{
  float width = sqrt(accessibility);
  blurx = vec2(width, 0);
  blury = vec2(0, width);
  
  return vec4(blurx, blury);
}

float fGetGeometricalAttenuationFactor(vec3 normal, vec3 half, vec3 view, vec3 light)
{
   float fNDotH = max ( 0.0, dot( normal, half ) );
   float fNDotL = max ( 0.0, dot( normal, light ) );
   float fNDotV = max ( 0.0, dot( normal, view ) );
   float fVDotH = max ( 0.0, dot( view, half ) );
   
   float a = 2.0 * fNDotH * fNDotV / fVDotH;
   float b = 2.0 * fNDotH * fNDotL / fVDotH;
   
   return min( 1.0, min( a, b ) );
}

float fGetFresnelFactor(float fresnelBase, vec3 half, vec3 view)
{
  return fresnelBase + pow( 1.0 - dot( half, view ), 5.0 ) * (1.0 - fresnelBase);
}
